package com.kexun.handler;

import org.apache.shiro.authz.AuthorizationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

@ControllerAdvice
public class GlobalExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(AuthorizationException.class)
    public String onAuthException(Exception e, HttpServletRequest request) {
        HashMap<String, Object> res = new HashMap<>();
        res.put("message", "无权访问,如有需要请联系管理员");
        res.put("code", -1);
        request.setAttribute("javax.servlet.error.status_code", 200);
        request.setAttribute("error", res);
        return "forward:/error";
    }

    @ExceptionHandler(ApiRRException.class)
    public String apiException(ApiRRException e, HttpServletRequest request) {
        HashMap<String, Object> res = new HashMap<>();
        res.put("message", e.getErrmsg());
        res.put("code", -1);
        request.setAttribute("javax.servlet.error.status_code", 200);
        request.setAttribute("error", res);
        return "forward:/error";
    }


    @ExceptionHandler(Exception.class)
    public String Exception(Exception e, HttpServletRequest request) {
        e.printStackTrace();
        HashMap<String, Object> res = new HashMap<>();
        res.put("message", "系统异常" + e.getMessage());
        res.put("code", -1);
        request.setAttribute("javax.servlet.error.status_code", 200);
        request.setAttribute("error", res);
        return "forward:/error";
    }


}
